package com.twq.servlet;

import com.twq.pojo.Persion;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionDemo01 extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        解决乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
//得到session
        HttpSession session = req.getSession();

//        给session中存东西
        session.setAttribute("name",new Persion("秦疆",1));
//        获取session的ID

        String sessionId = session.getId();
//        判断session是否新创建
        if(session.isNew()){
            resp.getWriter().write("session创建成功，ID"+sessionId);
        }else {
            resp.getWriter().write("session已经在服务器中存在，ID"+sessionId);
//session在创建的时候做了什么
//            Cookie cookie = new Cookie("JSESSIONID",sessionId);
//            resp.addCookie(cookie);
        }



    }



}
